package cn.iocoder.yudao.module.promotion.combination.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.product.sku.ProductSkuApi;
import cn.iocoder.yudao.module.api.product.sku.dto.ProductSkuRespDTO;
import cn.iocoder.yudao.module.api.product.spu.ProductSpuApi;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuDetailRespDTO;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.api.promotion.activity.enums.PromotionTypeEnum;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.promotion.activity.convert.ActivityConvert;
import cn.iocoder.yudao.module.promotion.combination.convert.CombinationActivityConvert;
import cn.iocoder.yudao.module.promotion.combination.dal.dataobject.CombinationActivityDO;
import cn.iocoder.yudao.module.promotion.combination.dal.dataobject.CombinationProductDO;
import cn.iocoder.yudao.module.promotion.combination.service.CombinationActivityService;
import cn.iocoder.yudao.module.promotion.combination.vo.AppCombinationActivityProductDetailRespVO;
import cn.iocoder.yudao.module.promotion.combination.vo.AppCombinationActivityProductRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "用户 APP - 拼团活动")
@RestController
@RequestMapping("/promotion/combination-activity")
@Validated
public class AppCombinationActivityController {

    @Resource
    private CombinationActivityService activityService;

    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private ProductSpuApi productSpuApi;
    @Resource
    private ProductSkuApi productSkuApi;

    @GetMapping("/page")
    @Operation(summary = "获得拼团活动分页")
    @PermitAll
    public CommonResult<PageResult<AppCombinationActivityProductRespVO>> getCombinationActivityPage(PageParam pageParam) {
        PageResult<CombinationActivityDO> pageResult = activityService.getCombinationActivityPage(pageParam);
        List<CombinationActivityDO> activityList = pageResult.getList();
        if (CollUtil.isEmpty(activityList)) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        // 拼接返回
        return success(new PageResult<>(toActivityProductList(activityList), pageResult.getTotal()));
    }

    @GetMapping("/list-by-ids")
    @Operation(summary = "获得拼团活动列表，基于活动编号数组")
    @Parameter(name = "ids", description = "活动编号数组", required = true, example = "[1024, 1025]")
    @PermitAll
    public CommonResult<List<AppCombinationActivityProductRespVO>> getCombinationActivityListByIds(@RequestParam("ids") List<Long> ids) {
        // 1. 获得开启的活动列表
        List<CombinationActivityDO> activityList = activityService.getCombinationActivityListByIds(ids);
        activityList.removeIf(activity -> CommonStatusEnum.isDisable(activity.getStatus()));
        if (CollUtil.isEmpty(activityList)) {
            return success(Collections.emptyList());
        }
        // 2. 拼接返回
        return success(toActivityProductList(activityList));
    }

    @GetMapping("/get-detail")
    @Operation(summary = "获得拼团活动明细")
    @Parameter(name = "activityId", description = "活动编号", required = true, example = "1024")
    @Parameter(name = "skuId", description = "产品 SKU 编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<AppCombinationActivityProductDetailRespVO> getCombinationActivityDetail(@RequestParam("activityId") Long activityId, @RequestParam("skuId") Long skuId) {
        // 1. 获取活动
        CombinationActivityDO activity = activityService.getCombinationActivity(activityId);
        if (activity == null || ObjectUtil.equal(activity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
            return success(null);
        }

        // 2. 获取活动产品
        CombinationProductDO product = activityService.getCombinationProductByActivityIdAndSkuId(activity.getId(), skuId);
        if (product == null) {
            return success(null);
        }
        AppCombinationActivityProductDetailRespVO data = CombinationActivityConvert.INSTANCE.convert2(activity);
        data.setActivityId(activityId);
        data.setActivity(ActivityConvert.INSTANCE.convert(activity, PromotionTypeEnum.SECKILL_ACTIVITY.getValue()));
        data.setShop(shopInfoApi.getShopInfo(product.getShopId()));
        ProductSpuDetailRespDTO spu = productSpuApi.getSpuDetail(product.getSpuId());
        data.setSpu(spu);
        ProductSkuRespDTO sku = productSkuApi.getSku(skuId);
        data.setSku(sku);
        ActivityConvert.INSTANCE.convert(data, spu, sku);
        data.setCombinationPrice(product.getCombinationPrice());
        return success(data);
    }

    private List<AppCombinationActivityProductRespVO> toActivityProductList(List<CombinationActivityDO> activityList) {
        List<CombinationProductDO> productList = activityService.getCombinationProductListByActivityIds(convertList(activityList, CombinationActivityDO::getId));
        List<ProductSpuRespDTO> spuList = productSpuApi.getSpuList(convertList(productList, CombinationProductDO::getSpuId));
        List<ProductSkuRespDTO> skuList = productSkuApi.getSkuList(convertList(productList, CombinationProductDO::getSkuId));
        return CombinationActivityConvert.INSTANCE.convertAppList(productList, activityList, spuList, skuList);
    }

}
